package com.cldz.service;

import com.cldz.common.dto.res.DeviceResDto;
import com.cldz.dao.DeviceMapper;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.common.dto.req.DeviceDto;
import com.cldz.entity.Device;
import com.cldz.utils.BeanCopyUtil;
import com.cldz.utils.MathUtils;
import com.cldz.response.support.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 14:25 2018/12/4
 * @ Description：
 * @ Modified By：
 */
@Service("devListService")
@Slf4j
public class DevListService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private UserPropertyMapper userPropertyMapper;

    public ServerResponse<List<DeviceResDto>> getDevListByAreaID(DeviceDto deviceDto){
        setAreaID(deviceDto);

        List<Device> devListByAreaID = deviceMapper.getDevListByAreaID(deviceDto);

        List<DeviceResDto> list = copyListBean(devListByAreaID);

        Integer count = deviceMapper.getDevListByAreaIDCount(deviceDto);
        return ServerResponse.createDataAndCount(count,list);
    }

    public List<DeviceResDto> getDevicesInfoWithStation(DeviceDto deviceDto) {
        List<Device> list = deviceMapper.getDevicesInfoWithStation(deviceDto);

        return copyListBean(list);
    }

    public Integer getDevicesInfoWithStationAmount(DeviceDto deviceDto) {
        return deviceMapper.getDevicesInfoWithStationAmount(deviceDto);
    }

    public DeviceDto getDevByID(String devID) {
        Device device = deviceMapper.getDevByID(devID);
        DeviceDto deviceDto = new DeviceDto();
        if(device != null){
            BeanCopyUtil.copyPropertiesIgnoreNull(device,deviceDto);
        }
        return deviceDto;
    }

    public List<DeviceResDto> getDevListByTypeID(DeviceDto deviceDto){
        List<Device> devListByTypeID = deviceMapper.getDevListByTypeID(deviceDto);
        return copyListBean(devListByTypeID);
    }

    public List<DeviceResDto> getPassiveDeviceByInfo(DeviceDto deviceDto){
        List<Device> devices = deviceMapper.getPassiveDeviceByInfo(deviceDto);
        return copyListBean(devices);
    }


    public boolean isExistDev(DeviceDto deviceDto){
        Integer amount = deviceMapper.isExistDev(deviceDto);
        if(amount != null && amount != 0){
            return true;
        }else {
            return false;
        }
    }

    public List<DeviceResDto> getDeviceTypes(DeviceDto deviceDto){
        setAreaID(deviceDto);

        List<Device> deviceTypes = deviceMapper.getDeviceTypes(deviceDto);
        return copyListBean(deviceTypes);
    }

    private void setAreaID(DeviceDto deviceDto) {
        Integer areaID = deviceDto.getAreaID();
        if (areaID == null) {
            areaID = userPropertyMapper.queryAreaIDByUserID(deviceDto.getUserID());
        }
        deviceDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));
    }

    public List<Map<String, Object>> censusWithDeviceType(DeviceDto deviceDto) {
        List<Map<String,Object>> list = deviceMapper.censusWithDeviceType(deviceDto);
        return list;
    }

    public List<Map<String,Object>> getDeviceStatus(DeviceDto deviceDto) {
        String strDevIDs = deviceDto.getDeviceIDs();
        deviceDto.setStrDevIDs(strDevIDs.replaceAll(",","','"));
        List<Map<String,Object>> list = deviceMapper.getDeviceStatus(deviceDto);
        if(!CollectionUtils.isEmpty(list)){
            for (Map<String,Object> map : list) {
                if((Integer) map.get("status") ==  1){
                    map.put("status","禁用");
                }else {
                    if((Integer) map.get("onOff") ==  0){
                        map.put("status","离线");
                    }else if((Integer) map.get("onOff") ==  1){
                        map.put("status","在线");
                    }else if((Integer) map.get("onOff") ==  2){
                        map.put("status","异常");
                    }
                }
                map.remove("onOff");
            }
        }

        return list;
    }


    private List<DeviceResDto> copyListBean(List<Device> devListByTypeID) {
        List<DeviceResDto> list = new ArrayList<>();
        for (Device device : devListByTypeID) {
            DeviceResDto dto = new DeviceResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }



}
